package ru.usu.gv.utils.geometry;

import org.junit.Test;

import junit.framework.Assert;

/**
 * @author spupyrev
 * 29.10.2009
 */
public class GeometryTest
{
	@Test
	public void testIntersection()
	{
		Polyline rectPolyline = new Polyline();
		rectPolyline.addPoint(0, 0);
		rectPolyline.addPoint(0, 1);
		rectPolyline.addPoint(1, 1);
		rectPolyline.addPoint(1, 0);
		Polygon rectangle = new Polygon(rectPolyline);

		Assert.assertTrue(rectangle.intersect(createSegment(-0.25, 0.5, 0.5, 1.25)));
		Assert.assertFalse(rectangle.intersect(createSegment(-0.25, 0.75, 0.0, 1.25)));
		Assert.assertFalse(rectangle.intersect(createSegment(-0.25, 0.75, 0.25, 1.25)));
		Assert.assertFalse(rectangle.intersect(createSegment(0.1, 1, 0.9, 1)));
		Assert.assertFalse(rectangle.intersect(createSegment(0, 1, 1, 1)));
		Assert.assertFalse(rectangle.intersect(createSegment(-0.6, 1, 1.3, 1)));
		Assert.assertTrue(rectangle.intersect(createSegment(-0.6, 1, 1.3, 0.999)));
		Assert.assertTrue(rectangle.intersect(createSegment(0.25, 0.5, 0.75, 0.5)));
		Assert.assertTrue(rectangle.intersect(createSegment(0.5, 0.5, 1.25, 1.25)));
		Assert.assertTrue(rectangle.intersect(createSegment(0.5, 0.5, 1, 1)));
		Assert.assertFalse(rectangle.intersect(createSegment(2, 2, 1, 1)));
	}

	private Polyline createSegment(double x1, double y1, double x2, double y2)
	{
		Polyline segment = new Polyline();
		segment.addPoint(x1, y1);
		segment.addPoint(x2, y2);
		return segment;
	}
}
